package com.zhao.fishingguideserver.service.impl;

import com.zhao.fishingguideserver.POJO.Favorite;
import com.zhao.fishingguideserver.mapper.FavoriteMapper;
import com.zhao.fishingguideserver.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收藏服务实现类
 */
@Service
public class FavoriteServiceImpl implements FavoriteService {
    
    @Autowired
    private FavoriteMapper favoriteMapper;
    
    @Override
    public String addFavorite(Long userId, Long productId) {
        // 检查是否已收藏
        int count = favoriteMapper.checkFavorite(userId, productId);
        if (count > 0) {
            return "已经收藏过该商品";
        }
        
        Favorite favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setProductId(productId);
        
        int result = favoriteMapper.insert(favorite);
        return result > 0 ? "收藏成功" : "收藏失败";
    }
    
    @Override
    public String removeFavorite(Long userId, Long productId) {
        int result = favoriteMapper.deleteByUserIdAndProductId(userId, productId);
        return result > 0 ? "取消收藏成功" : "取消收藏失败";
    }
    
    @Override
    public List<Favorite> selectByUserId(Long userId) {
        return favoriteMapper.selectByUserId(userId);
    }
    
    @Override
    public boolean checkFavorite(Long userId, Long productId) {
        int count = favoriteMapper.checkFavorite(userId, productId);
        return count > 0;
    }
    
    @Override
    public int countByProductId(Long productId) {
        return favoriteMapper.countByProductId(productId);
    }
}

